package threads;/*
 *@author wenqh
 *@date 2024/4/22 8:58
 *@Description
 */

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class MyCallable implements Callable<Integer> {
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = 0; i <= 100; i++) {
            sum += i;
        }
        return sum;
    }

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /**
         * 特点：可获取线程执行结果
         *
         * 1、创建一个类，实现Callable接口
         * 2、重写call()
         *
         * 3、创建MyCallable实例（多线程要执行的任务）
         * 4、创建FetureTask实例（管理多线程运行的结果）
         * 5、创建Thread实例，并启动（表示线程）
         */
        MyCallable myCallable = new MyCallable();
        FutureTask<Integer> ft = new FutureTask<>(myCallable);

        Thread t1 = new Thread(ft);
        t1.start();

        Integer result = ft.get();
        System.out.println(result);

    }





}
